package com.quan.system.web.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.quan.common.base.bean.PageBean;
import com.quan.common.base.bean.ResultBean;
import com.quan.common.core.BaseController;
import com.quan.system.entity.JobTask;
import com.quan.system.service.JobTaskService;

/**
 * <p>
 * 定时任务 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2018-09-14
 */
@RestController
@RequestMapping("/system/task")
public class JobTaskController extends BaseController {

	@Autowired
	private JobTaskService service;

	/**
	 * 添加
	 * 
	 * @param entity
	 *            实体
	 * @return ResultBean
	 */
	@PostMapping("/add")
	public Object add(@Valid @RequestBody JobTask entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultBean.warning(bindingResult.getFieldError().getRejectedValue().toString());
		}
		JobTask insertSelective = this.service.insertSelective(entity);
		if (null != insertSelective) {
			return ResultBean.ok();
		} else {
			return ResultBean.failure();
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 *            ID
	 * @return ResultBean
	 */
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		int count = this.service.deleteById(id);
		if (count > 0) {
			return ResultBean.ok();
		} else {
			return ResultBean.failure("通过ID删除失败：[ " + id + " ]");
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param params
	 *            idList(JSON):["1","2"]
	 * @return ResultBean
	 */
	@PostMapping("/delete")
	public Object delete(@RequestBody Map<String, Object> params) {
		if (null != params && !params.isEmpty()) {
			List<Integer> idList = JSON.parseArray(params.get("idList").toString(), Integer.class);
			int count = this.service.deleteByIds(StringUtils.join(idList, ","));
			if (count > 0) {
				return ResultBean.ok();
			} else {
				return ResultBean.failure("通过ID删除失败：[ " + idList + " ]");
			}
		}
		return ResultBean.failure("请选项需要删除的记录");
	}

	/**
	 * 修改
	 * 
	 * @param entity
	 *            实体
	 * @return ResultBean
	 */
	@PostMapping("/update")
	public Object update(@Valid @RequestBody JobTask entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return ResultBean.warning(bindingResult.getFieldError().getDefaultMessage().toString());
		}
		int count = this.service.updateSelective(entity);
		if (count > 0) {
			return ResultBean.ok();
		} else {
			return ResultBean.failure();
		}
	}

	/**
	 * 通过ID查询
	 *
	 * @param id
	 *            ID
	 * @return JobTask
	 */
	@GetMapping("/get/{id}")
	public Object get(@PathVariable Integer id) {
		JobTask entity = this.service.selectOneById(id);
		if (null != entity) {
			return ResultBean.ok(entity);
		} else {
			return ResultBean.failure("通过ID查询失败：[ " + id + " ]");
		}
	}

	/**
	 * 多条件查询数据列表
	 *
	 * @param entiy
	 *            实体对象（查询条件）
	 * @return 分页对象
	 */
	@GetMapping(value = "/list")
	public Object list(JobTask entity) {
		return ResultBean.ok(this.service.selectAll(entity));
	}

	/**
	 * 多条件查询分页数据
	 *
	 * @param entiy
	 *            实体对象（查询条件）
	 * @return 分页对象
	 */
	@GetMapping(value = "/page")
	public Object page(JobTask entity) {
		return ResultBean.ok(this.service.selectPage(entity, new PageBean(request)));
	}

}
